from meza import io
import csv

data = io.read_mdb('/home/chaomaer/data.mdb')

# write the header
# FWLB (reside non-reside) (CS_AT) (CS_HI) CJ HX DATE0
def e_to_float(f):
    if f == '':
        return 0.0
    else: return float(f)

def useful(d):
    return d['J'], d['FWLB'], d['CS_AT'] + '/'+ d['CS_HI'], e_to_float(d['CJ']), d['HX'], e_to_float(d['JZMJ']), d['CQDW']

import happybase

table_name = 'house-table'
connection = happybase.Connection('hbase-docker', 9090)

# table = connection.create_table(table_name, families={'family': dict()})
print(connection.tables())

table = connection.table(table_name)

def byte_dict(record):
    return {
        b'family:category': record[1].encode(),
        b'family:floor': record[2].encode(),
        b'family:price:': str(record[3]).encode(),
        b'family:room': record[4].encode(),
        b'family:space': str(record[5]).encode(),
        b'family:band': record[6].encode(),
    }

def write2HBase(record):
    table.put(record[0], byte_dict(record))

for d in data:
    write2HBase(useful(d))

print("OK")
connection.close()


# f = open('data.csv', 'w')
# fd = csv.writer(f, delimiter =",",quoting=csv.QUOTE_MINIMAL)
#
# r = list(next(data).keys())
# fd.writerow(r)
#
# # write 100 items
#
# for _ in range(90000):
#     r = list(next(data).values())
#     fd.writerow(r)
